<?php 

require("../include/config.php");
require("../API/php/fonctions.php");
require("../API/php/header.php");

$modeDebug = false;

// phpsql/mysql_getTicketsSupDebMail.php?milis=123456789&ctrl=ok&code_user=

//[x][0]responsable 
//[x][1]nom 
//[x][2]prenom 
//[x][3]mail 

// IN obligatoire
$arrayGet = array(
    "ctrl" => null,
    "code_user" => null
);

$arrayValeur = recupGet($arrayGet, $bolDecode);

if($arrayValeur["error"] == null){
	// CONNECION SQL
	$db = mysql_connect($host, $base, $mdp)  or die('Erreur de connexion '.mysql_error());
        mysql_query("SET NAMES 'utf8'");

	// CONNECION BASE
	mysql_select_db($base,$db)  or die('Erreur de selection '.mysql_error());
        
        $strFiltreResponsable = "";
        if($arrayValeur["code_user"] != ""){
            $strFiltreResponsable = " AND responsable = '".$arrayValeur["code_user"]."'";
        }
	
	$sql = "SELECT DISTINCT
            a.responsable, b.nom, b.prenom, b.mail
            FROM `".$prefixTable."vm_inventaire_tocs` a, `".$prefixTable."tab_utilisateurs` b
            WHERE 1=1
            AND a.responsable = b.code_user
            AND a.statut = 'Support/Déblocage'
            AND a.emis != 'oui'
            AND a.responsable in (SELECT b.responsable FROM `effectifs` b WHERE 1=1
                    AND b.date_in != '0000-00-00' 
                    AND b.date_out != '0000-00-00')
            AND NOT EXISTS (select 1 from vm_inventaire_tocs_vsr z WHERE z.id_toc = a.id_toc)
            ".$strFiltreResponsable."
            ORDER BY responsable
	;";

	$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 
	
	// on fait une boucle qui va faire un tour pour chaque enregistrement 
	while($data = mysql_fetch_assoc($req)) 
	{ 
            $strSorti .= $data['responsable']
                .'&'.$data['nom']
                .'&'.$data['prenom']
                .'&'.$data['mail']
                .'&'
                ."\r\n"; 
        }
        
	// DECONNECION SQL
	mysql_close();
	
	if($modeDebug){
		$strSorti .= ('<br><br><br><br>'.$sql);
	}
}else{
	$strSorti = "ERROR:".$arrayValeur["error"];
}

//require("../API/php/footer.php");

echo $strSorti;
?>